package com.gdd.videos.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangh     
 * @version V1.0.0
 * @projectName parent
 * @title     FFMpegTest   
 * @package    com.gdd.videos.common  
 * @date   2019/9/12 13:50  
 * @explain
 */
public class FFMpegTest {

    private String ffmpegExe;

    public FFMpegTest(String ffmpegExe) {
        this.ffmpegExe = ffmpegExe;
    }

    /**
     * 转换视屏
     */
    public void convertor(String inputPath,String outputPath) throws IOException {

        List<String> command = new ArrayList<String>();
        command.add(ffmpegExe);
        command.add("-i");
        command.add(inputPath);
        command.add(outputPath);
        ProcessBuilder builder = new ProcessBuilder(command);
        Process process = builder.start();

        // 释放资源
        InputStream inputStream = process.getErrorStream();
        InputStreamReader reader = new InputStreamReader(inputStream);
        BufferedReader buf = new BufferedReader(reader);
        String line = "";
        while ((line = buf.readLine()) != null){
        }

        if (buf != null){
            buf.close();
        }
        if (reader != null){
            reader.close();
        }
        if (inputStream != null){
            inputStream.close();
        }
    }

    public static void main(String[] args) {
        FFMpegTest ffMpeg = new FFMpegTest("E:\\gdd_videos_files\\ffmpeg\\bin\\ffmpeg.exe");
        try {
            ffMpeg.convertor("E:\\gdd_videos_files\\ffmpeg\\bin\\test.mp4","E:\\gdd_videos_files\\ffmpeg\\bin\\ok.avi");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
